/*
 * MIT License
 *
 * Copyright (c) 2023 北京凯特伟业科技有限公司
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.je.message.service;

import com.je.common.base.DynaBean;
import com.je.common.base.service.MetaRbacService;
import com.je.common.base.service.rpc.SystemSettingRpcService;
import com.je.common.base.util.MessageUtils;
import com.je.common.base.util.StringUtil;
import com.je.message.ResultMsgResp;
import com.je.message.exception.MessageSendException;
import com.je.message.vo.MessageVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;
import static com.je.message.service.AbstractThirdPartyPush.thirdPartyType.*;

public abstract class AbstractThirdPartyPush {

    public Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    public MetaRbacService metaRbacService;
    @Autowired
    public SystemSettingRpcService systemSettingRpcService;

    public enum thirdPartyType {
        DINGTALK,
        WECHAT,
        LARK,
        WELINK;
    }

    final String ALL_USER = "@all";

    public Boolean checkService(thirdPartyType type)  {
        String platform = systemSettingRpcService.findSettingValue("JE_PUSH_PLATFORM");

        switch (type) {
            case DINGTALK:
                if (!platform.contains(DINGTALK.name())) {
                    throw new MessageSendException("钉钉推送未配置，请确认是否设置了钉钉消息推送！");
                }
            case WECHAT:
                if (!platform.contains(WECHAT.name())) {
                    throw new MessageSendException("微信推送未配置，请确认是否设置了微信消息推送！");
                }
            case LARK:
                if (!platform.contains(LARK.name())) {
                    throw new MessageSendException("飞书推送未配置，请确认是否设置了飞书消息推送！");
                }
            case WELINK:
                if (!platform.contains(WELINK.name())) {
                    throw new MessageSendException("WELINK推送未配置，请确认是否设置了WELINK消息推送！");
                }
            default:
                return true;
        }
    }

    public List<String> getUserIds(MessageVo msgVo, thirdPartyType type)  {
        List userIdLists = new ArrayList<String>();
        String userIds = msgVo.getUserIds();
        List<DynaBean> cpUserList = null;
        if (type.equals(thirdPartyType.LARK)) {
//            cpUserList = thirdPartySyncRpcService.getUsers(ThirdPartyType.LARK);
        } else if (type.equals(thirdPartyType.WELINK)) {
//            cpUserList = thirdPartySyncRpcService.getUsers(ThirdPartyType.WELINK);
        } else if (type.equals(WECHAT)) {
//            cpUserList = thirdPartySyncRpcService.getUsers(ThirdPartyType.WECHAT);
        } else if (type.equals(DINGTALK)) {
//            cpUserList = thirdPartySyncRpcService.getUsers(ThirdPartyType.DINGTALK);
        }
        if (ALL_USER.equals(userIds)) {
            for (DynaBean dynaBean : cpUserList) {
                userIdLists.add(dynaBean.getStr("CPUSER_USER_ID"));
            }
            return userIdLists;
        }
        for (String userId : userIds.split(",")) {
            if (StringUtil.isEmpty(userId)) {
                continue;
            }
            for (DynaBean dynaBean : cpUserList) {
                if (userId.equals(dynaBean.getStr("CPUSER_BDYH"))) {
                    userIdLists.add(dynaBean.getStr("CPUSER_USER_ID"));
                }
            }
            return userIdLists;
        }
        if (userIdLists.size() <= 0) {
            throw new MessageSendException(MessageUtils.getMessage("message.personnel.hookup.information.isEmpty"));
        }
        return userIdLists;
    }

    public abstract ResultMsgResp send(MessageVo msg) ;


}
